package com.thinkfit.jy.activity;

import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.common.lib.activity.BaseActivity;
import com.common.lib.bean.CourseTypeBean;
import com.common.lib.bean.DeviceInfoBean;
import com.common.lib.bean.ProceduralTrainBean;
import com.common.lib.bean.VideoListBean;
import com.common.lib.manager.DataManager;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.thinkfit.jy.R;
import com.thinkfit.jy.adapter.TrainPatternAdapter;
import com.thinkfit.jy.contract.ProceduralTrainingListContract;
import com.thinkfit.jy.presenter.ProceduralTrainingListPresenter;
import com.thinkfit.jy.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class HeartRateRunActivity extends BaseActivity<ProceduralTrainingListContract.Presenter> implements ProceduralTrainingListContract.View {

    LinearLayout llSportBack,llDifficulty,llTrainTime;
    ConstraintLayout llTrainStart;
    TextView tvCreationTrain,tvRecommendTrain,tvDifficulty,tvTrainTime,
            tvTrainTitle,tvLevel,tvTrainInfoTime,tvTrainFrequency,tvCommendedUser;
    RecyclerView rvTrain;
    ScrollView svDifficulty;
    BarChart bc1;
    View vTag1,vTag2,vTag3;

    int trainItemType = 0;
    ArrayList<TextView> tvDifficultyList = new ArrayList<>();
    ArrayList<TextView> tvTimeList = new ArrayList<>();
    boolean isShowDifficulty = true;
    boolean isShowTrainTime = true;
    TrainPatternAdapter trainPatternAdapter;
    int difficultyId = 0;
    int timeId = 0;
    int minTime = 0;
    int maxTime = 0;

    protected int getLayoutId() {
        return R.layout.activity_heart_rate_run;
    }
    @NonNull
    @Override
    protected ProceduralTrainingListContract.Presenter onCreatePresenter() {
        return new ProceduralTrainingListPresenter(this);
    }
    @Override
    protected void onCreated(@Nullable Bundle savedInstanceState) {
        initView();
        getPresenter().getVideoList(difficultyId,minTime,maxTime);
    }

    public void initView(){
        llSportBack = findViewById(R.id.ll_sport_back);
        tvCreationTrain = findViewById(R.id.tv_creation_train);
        tvRecommendTrain = findViewById(R.id.tv_recommend_train);

        tvDifficulty = findViewById(R.id.tv_difficulty);
        tvTrainTime = findViewById(R.id.tv_train_time);
        rvTrain = findViewById(R.id.rv_train);
        svDifficulty = findViewById(R.id.sv_difficulty);
        llDifficulty = findViewById(R.id.ll_difficulty);
        llTrainTime = findViewById(R.id.ll_train_time);

        initRecycler();

        tvTrainTitle = findViewById(R.id.tv_train_title);
        tvLevel = findViewById(R.id.tv_level);
        tvTrainInfoTime = findViewById(R.id.tv_train_info_time);
        tvTrainFrequency = findViewById(R.id.tv_train_frequency);
        llTrainStart = findViewById(R.id.ll_train_start);
        tvCommendedUser = findViewById(R.id.tv_commended_user);
        bc1 = findViewById(R.id.bc_1);

//        setViewsOnClickListener(llSportBack,llTrainStart,tvCreationTrain,tvRecommendTrain,tvWarmStretch,tvMyTrain);
        llSportBack.setOnClickListener(this);
        llTrainStart.setOnClickListener(this);
        tvCreationTrain.setOnClickListener(this);
        tvRecommendTrain.setOnClickListener(this);
        tvDifficulty.setOnClickListener(this);
        tvTrainTime.setOnClickListener(this);

        initTimeList();
        initTime();
        initBarCher();
    }

    public void initBarCher(){
        bc1.getLegend().setEnabled(false);//不显示图例
        bc1.getDescription().setEnabled(false);//不显示描述

        XAxis xAxis = bc1.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//x轴所在位置
        xAxis.setAxisMinimum(0);//x轴最小值
        xAxis.setCenterAxisLabels(true);//x轴显示标签居中
        xAxis.setTextColor(getContext().getColor(com.common.lib.R.color.transparent));
        xAxis.setDrawGridLines(true);
        xAxis.setDrawAxisLine(true);
        xAxis.setGridColor(getContext().getColor(R.color.color_3A3A3A));


        YAxis axisLeft = bc1.getAxisLeft();
        axisLeft.setDrawAxisLine(false);
        axisLeft.setDrawGridLines(false);
        bc1.getAxisRight().setDrawGridLines(false);
        axisLeft.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        axisLeft.setAxisMinimum(0f);
        axisLeft.setTextColor(getContext().getColor(R.color.white));



//        bc1.setTouchEnabled(false);
        bc1.getAxisRight().setDrawGridLines(false);
        bc1.getAxisRight().setEnabled(false);



        Description description=new Description();
        description.setEnabled(false);
        bc1.setDescription(description);


    }



    public void initRecycler(){
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvTrain.setLayoutManager(linearLayoutManager);
        rvTrain.setAdapter(getAdapter());
    }

    public TrainPatternAdapter getAdapter(){
        if(trainPatternAdapter == null){
            trainPatternAdapter = new TrainPatternAdapter(this);
        }
        trainPatternAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                refeshTrainInfo(trainPatternAdapter.getItem(position));
            }
        });

        return trainPatternAdapter;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_sport_back:
                LogUtils.getInstance().l(LogUtils.HINT,"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx   ll_sport_back ");
                finish();
                break;
            case R.id.ll_train_start:

                break;
            case R.id.tv_creation_train:
                openActivity(CreateTrainPlanActivity.class);
                break;
            case R.id.tv_difficulty:
                if(isShowDifficulty){
                    isShowDifficulty = false;
                    svDifficulty.setVisibility(View.VISIBLE);
                    refreshDifficultyTextView(difficultyId);
                }else {
                    isShowDifficulty = true;
                    svDifficulty.setVisibility(View.GONE);
                }
                break;
            case R.id.tv_train_time:
                if(isShowTrainTime){
                    isShowTrainTime = false;
                    llTrainTime.setVisibility(View.VISIBLE);
                    refreshTimeTextView(timeId);
                }else {
                    isShowTrainTime = true;
                    llTrainTime.setVisibility(View.GONE);
                }
                break;
        }
    }

    ArrayList<CourseTypeBean> mTypelist = new ArrayList<>();
    ArrayList<CourseTypeBean> mTimelist = new ArrayList<>();

    public void refreshDifficultyTextView(int type){
        for (int i = 0; i < mTypelist.size(); i++){
            if(mTypelist.get(i).getId() == type){
                tvDifficultyList.get(i).setTextColor(getResources().getColor(R.color.white));
            }else {
                tvDifficultyList.get(i).setTextColor(getResources().getColor(com.common.lib.R.color.color_60_ff_ff_ff));
            }
        }
    }
    public void refreshTimeTextView(int type){
        for (int i = 0; i < mTimelist.size(); i++){
            if(mTimelist.get(i).getId() == type){
                tvTimeList.get(i).setTextColor(getResources().getColor(R.color.white));
            }else {
                tvTimeList.get(i).setTextColor(getResources().getColor(com.common.lib.R.color.color_60_ff_ff_ff));
            }
        }
    }

    public void refeshTrainInfo(VideoListBean bean){
        tvTrainInfoTime.setText(bean.getMin()+getString(R.string.app_minutes));
        tvLevel.setText(getLevel(bean.getStar()));
        if(DataManager.Companion.getInstance().getLanguage() == 0){
            tvTrainTitle.setText(bean.getName());
            tvCommendedUser.setText(Html.fromHtml(bean.getCrowd()));
        }else {
            tvTrainTitle.setText(bean.getEnname());
            tvCommendedUser.setText(Html.fromHtml(bean.getEncrowd()));
        }

        initBarCherData(bean.getBrustlist());
    }

    public void initBarCherData(List<DeviceInfoBean> brustlist){
        if(brustlist == null || brustlist.size() == 0){
            return;
        }
        LogUtils.getInstance().l(LogUtils.HINT,"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx   brustlist.size() : "+brustlist.size());
        ArrayList<BarEntry> slops = new ArrayList<>();
        ArrayList<BarEntry> speeds = new ArrayList<>();
        for (int i = 0; i < brustlist.size(); i++){
            slops.add(new BarEntry(i+1, Float.parseFloat(brustlist.get(i).getDrap()+"")));
            speeds.add(new BarEntry(i+1, Float.parseFloat(brustlist.get(i).getSpeed()+"")));
            LogUtils.getInstance().l(LogUtils.HINT,"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx   slop : "+brustlist.get(i).getSlope()+"   speed : "+brustlist.get(i).getSpeed());
        }
        BarDataSet slope = new BarDataSet(slops,getContext().getString(R.string.app_slop));
        BarDataSet speeds1 = new BarDataSet(speeds, getContext().getString(R.string.app_speed_txt));
        speeds1.setColor(getContext().getColor(R.color.color_4865B5));
//        speeds1.setBarBorderColor(getColor(R.color.color_4865B5));
        slope.setColor(getContext().getColor(R.color.color_3BC2AC));
//        slope.setBarBorderColor(getColor(R.color.color_3BC2AC));
        speeds1.setDrawValues(false);
        slope.setDrawValues(false);
        ;

//        BarData barData = new BarData(data());
        BarData barData = new BarData(speeds1,slope);
        bc1.setData(barData);


        float barspace = 0.2f;
        float groupspace = 0.2f;
        float barwidth = 0.2f;
        float granularity =(barwidth + barspace)*2 + groupspace;
        LogUtils.getInstance().l(LogUtils.HINT,"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx   granularity : "+granularity);
        bc1.setScaleEnabled(false);
        bc1.getXAxis().setGranularity(1.0f);
        bc1.setScaleX(granularity);

        bc1.groupBars(1.2f,groupspace,-0.6f);

        barData.setBarWidth(barwidth);

        bc1.invalidate();
    }

    public String getLevel(int level){
        String levelStr = "";
        switch (level){
            case 1:
                levelStr = getString(R.string.app_video_level_1);
                break;
            case 2:
                levelStr = getString(R.string.app_video_level_2);
                break;
        }
        return levelStr;
    }

    public void initTime(){
        if(tvTimeList.size() > 0){
            return;
        }
        tvTimeList.clear();
        llTrainTime.removeAllViews();
        for (int i = 0; i < mTimelist.size(); i++){
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 11, 0, 0); // left, top, right, bottom margins
            TextView textView = new TextView(getContext());
            textView.setTextSize(13);
            if(DataManager.Companion.getInstance().getLanguage() == 0){
                textView.setText(mTimelist.get(i).getName());
            }else {
                textView.setText(mTimelist.get(i).getEnname());
            }
            textView.setLayoutParams(params);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int y = 0;y<mTimelist.size();y++){
                        tvTimeList.get(y).setTextColor(getContext().getColor(com.common.lib.R.color.color_60_ff_ff_ff));
                        if(textView.getText().toString().equals(tvTimeList.get(y).getText().toString())){
                            timeId = mTimelist.get(y).getId();
                        }

                    }
                    refsehTime(timeId);
                    textView.setTextColor(getContext().getColor(R.color.white));
                    isShowTrainTime = true;
                    tvTrainTime.setText(textView.getText().toString().trim());
                    llTrainTime.setVisibility(View.GONE);
                    getPresenter().getVideoList(difficultyId,minTime,maxTime);
                }
            });
            tvTimeList.add(textView);
            llTrainTime.addView(textView);
        }
        tvTrainTime.setText(tvTimeList.get(0).getText().toString().trim());
    }

    public void initDifficulty(ArrayList<CourseTypeBean> typelist){
        if(tvDifficultyList.size() > 0){
            return;
        }
        mTypelist.clear();
        llDifficulty.removeAllViews();
        CourseTypeBean courseTypeBean = new CourseTypeBean();
        courseTypeBean.setId(0);
        courseTypeBean.setEnname("Type");
        courseTypeBean.setName("类型");
        mTypelist.add(courseTypeBean);
        for (int i = 0; i < typelist.size(); i++){
            mTypelist.add(typelist.get(i));
        }
        for (int i = 0; i < mTypelist.size(); i++){
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 11, 0, 0); // left, top, right, bottom margins
            TextView textView = new TextView(getContext());
            textView.setTextSize(13);
            if(DataManager.Companion.getInstance().getLanguage() == 0){
                textView.setText(mTypelist.get(i).getName());
            }else {
                textView.setText(mTypelist.get(i).getEnname());
            }
            textView.setLayoutParams(params);
            textView.setTextColor(getContext().getColor(com.common.lib.R.color.color_60_ff_ff_ff));
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int y = 0;y<mTypelist.size();y++){
                        tvDifficultyList.get(y).setTextColor(getContext().getColor(com.common.lib.R.color.color_60_ff_ff_ff));
                        if(textView.getText().toString().equals(tvDifficultyList.get(y).getText().toString())){
                            difficultyId = mTypelist.get(y).getId();

                        }

                    }
                    textView.setTextColor(getContext().getColor(R.color.white));
                    isShowDifficulty = true;
                    svDifficulty.setVisibility(View.GONE);
                    tvDifficulty.setText(textView.getText().toString().trim());
                    getPresenter().getVideoList(difficultyId,minTime,maxTime);
                }
            });
            tvDifficultyList.add(textView);
            llDifficulty.addView(textView);
        }
        tvDifficulty.setText(tvDifficultyList.get(0).getText().toString().trim());

    }

    @Override
    public void getVideoListSuccess(ProceduralTrainBean bean) {
        if(bean != null & bean.getTypelist() != null){
            initDifficulty(bean.getTypelist());
        }
        getAdapter().setNewInstance(bean.getVideoList());
        if(bean.getVideoList().size() >= 1 ){
            refeshTrainInfo(bean.getVideoList().get(0));
        }


    }

    @Override
    public void getWattinfoSuccess(VideoListBean bean) {

    }

    public void refsehTime(int timeId){
        switch (timeId){
            case 0:
                minTime = 0;
                maxTime = 0;
                break;
            case 1:
                minTime = 0;
                maxTime = 15;
                break;
            case 2:
                minTime = 16;
                maxTime = 30;
                break;
            case 3:
                minTime = 31;
                maxTime = 45;
                break;
            case 4:
                minTime = 46;
                maxTime = 999;
                break;

        }
    }

    public void initTimeList(){
        CourseTypeBean courseTypeBean = new CourseTypeBean();
        courseTypeBean.setId(0);
        courseTypeBean.setName("时间");
        courseTypeBean.setEnname("Time");
        mTimelist.add(courseTypeBean);
        CourseTypeBean courseTypeBean1 = new CourseTypeBean();
        courseTypeBean1.setId(1);
        courseTypeBean1.setName("15分钟");
        courseTypeBean1.setEnname("15min");
        mTimelist.add(courseTypeBean1);
        CourseTypeBean courseTypeBean2 = new CourseTypeBean();
        courseTypeBean2.setId(2);
        courseTypeBean2.setName("30分钟");
        courseTypeBean2.setEnname("30min");
        mTimelist.add(courseTypeBean2);
        CourseTypeBean courseTypeBean3 = new CourseTypeBean();
        courseTypeBean3.setId(3);
        courseTypeBean3.setName("45分钟");
        courseTypeBean3.setEnname("45min");
        mTimelist.add(courseTypeBean3);
        CourseTypeBean courseTypeBean4 = new CourseTypeBean();
        courseTypeBean4.setId(4);
        courseTypeBean4.setName("45分钟+");
        courseTypeBean4.setEnname("45min+");
        mTimelist.add(courseTypeBean4);

    }
}
